﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;
namespace HardWareInfoer
{
    class Program
    {
       
        public class UpdateVisitor : IVisitor
        {
            public void VisitComputer(IComputer computer)
            {
                computer.Traverse(this);
            }
            public void VisitHardware(IHardware hardware)
            {
                hardware.Update();
                foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
            }
            public void VisitSensor(ISensor sensor) { }
            public void VisitParameter(IParameter parameter) { }
        }
        static void GetSystemInfo()
        {
            UpdateVisitor updateVisitor = new UpdateVisitor();
            Computer computer = new Computer();
            computer.Open();
            computer.CPUEnabled = true;
            computer.GPUEnabled = true;
            computer.HDDEnabled = true;
            computer.RAMEnabled = true;
            computer.FanControllerEnabled = true;
            computer.MainboardEnabled = true;
            computer.Accept(updateVisitor);
            /*
             *  "cpuinfo":{
               "type":"i9-119000k",
               "usage":0.6,
               "temperature":57
               },

               "gpuInfo":{
	            "type":"RTX3090",
               "usage":0.8,
               "temperature":57,
               "memory":16,   //总显存大小
               "usedMemory":13 //已使用显存
               },

               "memInfo":{
               "type":"helix",
               "usage":0.8, 
               "memory":16, //内存大小
               },

               "hddInfo":{
                "type":"helix",
                "usage":0.8
               }
             * 
             */

            String cpuName="", gpuName="", memName="", hddName="";
            float cpuUsage=0, gpuUsage=0, cpuTmp=0, gpuTmp=0, gpuMem=0, gpuUsedMem=0, memUsage=1, memTotal=0,hddUsage=0,memUsedMem=0;
            for (int i = 0; i < computer.Hardware.Length; i++)
            {
                var hardwarename = computer.Hardware[i].Name;
                //CPU
                if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    cpuName = hardwarename;
                    Console.WriteLine("CPU型号:" + hardwarename + "\r");

                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if (type == SensorType.Temperature && name== "CPU Package")
                        {
                            cpuTmp = computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                            Console.WriteLine("CPU温度:" + computer.Hardware[i].Sensors[j].Value.ToString() + "℃\r");

                        }

                        if (type == SensorType.Load && name == "CPU Total")
                        {
                            cpuUsage= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                             Console.WriteLine("CPU使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }
                    }
                   // Console.WriteLine("\r");
                }

                //GPU
                if (computer.Hardware[i].HardwareType == HardwareType.GpuNvidia|| computer.Hardware[i].HardwareType == HardwareType.GpuAti)
                {
                    Console.WriteLine("\r");
                   gpuName = hardwarename;
                    Console.WriteLine("GPU型号:" + hardwarename + "\r");

                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if (type == SensorType.Temperature && name == "GPU Core")
                        {
                            gpuTmp= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                              Console.WriteLine("GPU温度:" + computer.Hardware[i].Sensors[j].Value.ToString() + "℃\r");
                        }

                        if (type == SensorType.Load && name == "GPU Core")
                        {
                            gpuUsage= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                             Console.WriteLine("GPU使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }

                        if (type == SensorType.Load && name == "GPU Memory")
                        {
                            Console.WriteLine("GPU显存使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }

                        if ( name == "GPU Memory Total")
                        {
                            gpuMem = computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();

                             Console.WriteLine("GPU显存:" + computer.Hardware[i].Sensors[j].Value.ToString() + "MB\r");
                        }

                        if (name == "GPU Memory Used")
                        {
                            gpuUsedMem= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                            Console.WriteLine("GPU已用显存:" + computer.Hardware[i].Sensors[j].Value.ToString() + "MB\r");
                        }

                    }

                }

                //RAM
                if (computer.Hardware[i].HardwareType == HardwareType.RAM)
                {
                    Console.WriteLine("\r");
                    memName = hardwarename;
                    Console.WriteLine("内存型号:" + hardwarename + "\r");

                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if ( name == "Used Memory")
                        {
                           memUsedMem= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                            Console.WriteLine("已用内存:" + computer.Hardware[i].Sensors[j].Value.ToString() + "GB\r");
                        }
                       if (name == "Available Memory")
                        {
                            Console.WriteLine("可用内存:" + computer.Hardware[i].Sensors[j].Value.ToString() + "GB\r");
                        }

                        if (type == SensorType.Load)
                        {
                            memUsage= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                             Console.WriteLine("内存使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }
                    }
                   // Console.WriteLine("\r");

                }

                //HDD
                if (computer.Hardware[i].HardwareType == HardwareType.HDD)
                {
                    Console.WriteLine("\r");
                    hddName = hardwarename;
                    Console.WriteLine("硬盘型号:" + hardwarename + "\r");
                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if (type == SensorType.Load && name == "Used Space")
                        {
                            hddUsage= computer.Hardware[i].Sensors[j].Value.GetValueOrDefault();
                             Console.WriteLine("硬盘使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }
                    }
                   // Console.WriteLine("\r");

                }


                //FAN
                if (computer.Hardware[i].HardwareType == HardwareType.Heatmaster)
                {
                    Console.WriteLine("\r");
                    hddName = hardwarename;
                    Console.WriteLine("风扇型号:" + hardwarename + "\r");
                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if (type == SensorType.Fan)
                        {
                            Console.WriteLine("风扇转速:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }
                    }
                    // Console.WriteLine("\r");

                }

                //MainBorad
                if (computer.Hardware[i].HardwareType == HardwareType.Mainboard)
                {
                    Console.WriteLine("\r");
                    hddName = hardwarename;
                    Console.WriteLine("主板型号:" + hardwarename + "\r");
                  /*  for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        var type = computer.Hardware[i].Sensors[j].SensorType;
                        var name = computer.Hardware[i].Sensors[j].Name;
                        if (type == SensorType.Load && name == "Used Space")
                        {
                           
                            Console.WriteLine("硬盘使用率:" + computer.Hardware[i].Sensors[j].Value.ToString() + "%\r");
                        }
                    }*/
                    // Console.WriteLine("\r");

                }



            }
            computer.Close();
            if (memUsage != 0) memTotal = memUsedMem /( memUsage/100);
            String output = "{\"cpuinfo\":{ \"type\":\"" + cpuName + "\",\"usage\":" + cpuUsage + ",\"temperature\":" + cpuTmp + "},\"gpuInfo\":{\"type\":\"" + gpuName +
                "\",\"usage\":" + gpuUsage + ",\"temperature\":" + gpuTmp + ",\"memory\":" + gpuMem + ",\"usedMemory\":" + gpuUsedMem + "}," +
                "\"memInfo\":{ \"type\":\"" + memName + "\",\"usage\":" + memUsage + ",\"memory\":" + memTotal + "}," +
                "\"hddInfo\":{\"type\":\"" + hddName + "\",\"usage\":" + hddUsage + "}}";

            Console.WriteLine(output);

        }
        static void Main(string[] args)
        {
          //  while (true)
            {
                GetSystemInfo();
               // Console.ReadLine();
            }
            
        }
    }
}

